<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>One free point localization</title>
<link rel="canonical" href="http://cvxr.com/cvx/examples/cvxbook/Ch08_geometric_probs/html/ex_8_5.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>One free point localization</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
Plots
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% Section 8.7.3, Boyd &amp; Vandenberghe "Convex Optimization"</span>
<span class="comment">% Joelle Skaf - 10/24/05</span>
<span class="comment">%</span>
<span class="comment">% K fixed points x_1,...,x_K in R^2 are given and the goal is to place</span>
<span class="comment">% one additional point x such that the sum of the squares of the</span>
<span class="comment">% Euclidean distances to fixed points is minimized:</span>
<span class="comment">%           minimize    sum_{i=1}^K  ||x - x_i||^2</span>
<span class="comment">% The optimal point is the average of the given fixed points</span>

<span class="comment">% Data generation</span>
n = 2;
K = 11;
randn(<span class="string">'state'</span>,0);
P = randn(n,K);

<span class="comment">% minimizing the sum of Euclidean distance</span>
fprintf(1,<span class="string">'Minimizing the sum of the squares the distances to fixed points...'</span>);

cvx_begin
    variable <span class="string">x(2)</span>
    minimize ( sum( square_pos( norms(x*ones(1,K) - P,2) ) ) )
cvx_end

fprintf(1,<span class="string">'Done! \n'</span>);

<span class="comment">% Displaying results</span>
disp(<span class="string">'------------------------------------------------------------------'</span>);
disp(<span class="string">'The optimal point location is: '</span>);
disp(x);
disp(<span class="string">'The average location of the fixed points is'</span>);
disp(sum(P,2)/K);
disp(<span class="string">'They are the same as expected!'</span>);
</pre>
<a id="output"></a>
<pre class="codeoutput">
Minimizing the sum of the squares the distances to fixed points... 
Calling SDPT3: 88 variables, 46 equality constraints
   For improved efficiency, SDPT3 is solving the dual problem.
------------------------------------------------------------

 num. of constraints = 46
 dim. of sdp    var  = 22,   num. of sdp  blk  = 11
 dim. of socp   var  = 33,   num. of socp blk  = 11
 dim. of linear var  = 22
*******************************************************************
   SDPT3: Infeasible path-following algorithms
*******************************************************************
 version  predcorr  gam  expon  scale_data
   HKM      1      0.000   1        0    
it pstep dstep pinfeas dinfeas  gap      prim-obj      dual-obj    cputime
-------------------------------------------------------------------
 0|0.000|0.000|1.0e+01|1.1e+01|4.6e+03| 1.100000e+02  0.000000e+00| 0:0:00| chol  1  1 
 1|0.854|0.860|1.5e+00|1.6e+00|9.5e+02| 8.596987e+01 -1.058415e+02| 0:0:00| chol  1  1 
 2|1.000|1.000|1.9e-06|1.0e-02|1.3e+02| 4.110764e+01 -8.490538e+01| 0:0:00| chol  1  1 
 3|0.978|0.982|9.4e-07|1.2e-03|2.4e+01|-7.230670e+00 -3.138282e+01| 0:0:00| chol  1  1 
 4|1.000|1.000|2.2e-07|1.0e-04|8.5e+00|-1.347951e+01 -2.199622e+01| 0:0:00| chol  1  1 
 5|0.912|0.910|3.5e-08|1.8e-05|1.1e+00|-1.633531e+01 -1.742632e+01| 0:0:00| chol  1  1 
 6|1.000|1.000|9.6e-09|1.0e-06|3.3e-01|-1.657765e+01 -1.691125e+01| 0:0:00| chol  1  1 
 7|0.956|0.956|3.9e-09|1.4e-07|1.7e-02|-1.667823e+01 -1.669482e+01| 0:0:00| chol  1  1 
 8|0.984|0.984|1.7e-09|1.3e-08|2.7e-04|-1.668304e+01 -1.668331e+01| 0:0:00| chol  1  1 
 9|0.989|0.989|3.3e-11|4.9e-10|3.1e-06|-1.668312e+01 -1.668312e+01| 0:0:00| chol  1  1 
10|0.996|0.995|2.0e-13|9.4e-12|4.0e-08|-1.668312e+01 -1.668312e+01| 0:0:00|
  stop: max(relative gap, infeasibilities) &lt; 1.49e-08
-------------------------------------------------------------------
 number of iterations   = 10
 primal objective value = -1.66831188e+01
 dual   objective value = -1.66831189e+01
 gap := trace(XZ)       = 4.01e-08
 relative gap           = 1.17e-09
 actual relative gap    = 1.14e-09
 rel. primal infeas     = 1.95e-13
 rel. dual   infeas     = 9.35e-12
 norm(X), norm(y), norm(Z) = 1.9e+01, 8.1e+00, 1.1e+01
 norm(A), norm(b), norm(C) = 1.1e+01, 4.3e+00, 6.3e+00
 Total CPU time (secs)  = 0.22  
 CPU time per iteration = 0.02  
 termination code       =  0
 DIMACS: 4.2e-13  0.0e+00  2.0e-11  0.0e+00  1.1e-09  1.2e-09
-------------------------------------------------------------------
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +16.6831
 
Done! 
------------------------------------------------------------------
The optimal point location is: 
    0.0379
    0.0785

The average location of the fixed points is
    0.0379
    0.0785

They are the same as expected!
</pre>
</div>
</body>
</html>